<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<section class="tabs grid_12 leading">
    <ul class="clearfix">
        <s:iterator value="#request.organizations" var="orgs" status="st">
            <s:if test="#request.orgs.id == #request.organizationId">
                <li><a href="javascript:void(0)"
                       onclick="loadChildrenOrgsByTab(${orgs.id})" class="current">${orgs.fullName}</a></li>
            </s:if>
            <s:else>
                <li><a href="javascript:void(0)"
                       onclick="loadChildrenOrgsByTab(${orgs.id})">${orgs.fullName}</a></li>
            </s:else>
        </s:iterator>
    </ul>
    <section>
        <s:iterator value="#request.organizations" var="orgs" status="st">
        <s:if test="#st.index == 0">
        <section class="clearfix" style="display: block;">
            </s:if>
            <s:else>
            <section class="clearfix" style="display: none;">
                </s:else>
                <s:if test="#request.module == 'examaward'">
                        <header class="grid_12 alpha omega">
                            <p>
                                <span>年月： </span>
                                <select id="search_year_${orgs.id}">
                                    <s:iterator begin="#request.startYear" end="#request.endYear" var="y">
                                        <option value="${y}">${y}年</option>
                                    </s:iterator>
                                </select>
                                <select id="search_month_${orgs.id}">
                                    <s:iterator begin="1" end="12" var="m">
                                        <option value="${m}">${m}月</option>
                                    </s:iterator>
                                </select>
                            </p>
                        </header>
                        
                        <input type="button" value="点击搜索"
	                                      onclick="javascript:searchAwardClickEvent()">
                </s:if>
                <s:if test="#request.showChildOrg">
                    <s:if test="#request.showYearMonth">
                        <header class="grid_12 alpha omega">
                            <p>
                                <span>年月： </span>
                                <select id="search_year">
                                    <s:iterator begin="#request.startYear" end="#request.endYear" var="y">
                                        <option value="${y}" ${y==year?"selected='selected'":""}>${y}年</option>
                                    </s:iterator>
                                </select>
                                <select id="search_month">
                                    <s:iterator begin="1" end="12" var="m">
                                        <option value="${m}" ${m==month?"selected='selected'":""}>${m}月</option>
                                    </s:iterator>
                                </select>
                            </p>
                        </header>
                    </s:if>
                    <header class="grid_12 alpha omega">
                        <p>
                            <span>筛选： </span>
                            <s:if test="#request.module == 'salary_balance'">
                            <span>
                            	<select id="search_year_${orgs.id}" name="year">
                                    <s:iterator begin="#request.startYear" end="#request.endYear" var="y">
                                        <option value="${y}" ${y==year?"selected='selected'":""}>${y}年</option>
                                    </s:iterator>
                                </select>
		
		                        <select id="search_month_${orgs.id}" name="month">
                                    <s:iterator begin="1" end="12" var="m">
                                        <option value="${m}" ${m==month?"selected='selected'":""}>${m}月</option>
                                    </s:iterator>
                                </select>
                            </span>
                            </s:if>
                            <s:if test="#request.showDateInputSearch == true">
                            <span>
                                开始日期：<input id="startDate" name="startDateStr" onfocus="WdatePicker({maxDate:$('#endDate').val()})" type="text" style="width: 80px" value="${startDateStr}"/>
                                结束日期：<input id="endDate" name="endDateStr" onfocus="WdatePicker({minDate:$('#startDate').val()})" type="text" style="width: 80px" value="${endDateStr}"/>
                            </span>
                            </s:if>
                            <span id="org_sel_${orgs.id}"></span> 
                            <s:if test="#request.module != 'train'">
	                            <span>
									<select id="duty_sel_${orgs.id}" style="opacity: 0;"
		                                    onchange="dutyChangeEvent(this)">
	                                	<option value="-1">所有岗位</option>
	                            	</select>
								</span>
							</s:if>
                            <s:if test="#request.module == 'invite_arrange'">
							<span> <select id="arrange_sel_${orgs.id}"
                                           style="opacity: 0;">
                                <option value="-1">所有状态</option>
                                <option value="not_scheduled">未安排</option>
                                <option value="arrangement">安排中</option>
                                <option value="in">进行中</option>
                                <option value="completed">已完成</option>
                            </select>
							</span>
                            </s:if>
                            <s:if test="#request.module == 'qual_browse'">
							<span> <select id="periodtype_sel_${orgs.id}"
                                           style="opacity: 0;">
                                <option value="monthly">月度</option>
                                <option value="bimonthly">双月</option>
                                <option value="season">季度</option>
                                <option value="semiyearly">半年</option>
                                <option value="annually">一年</option>
                            </select>
							</span>
                            </s:if>
                        <s:if test="#request.module == 'salary_balance'">
                        	<span><select id="sel_employee_${orgs.id}" style="opacity: 0;">
	                            <option value="-1">所有人员</option>
                        	</select></span>
                        </s:if>
                        <s:if test="#request.module != null && #request.module.equals('train')">
	                        <span><select id="train_year_${orgs.id}" style="opacity: 0;">
		                            <option value="-1">所有年份</option>
		                            <s:iterator var="year" status="st" value="#request.searchyears">
		                            	<option value="<s:property/>"><s:property/></option>
		                            </s:iterator>
                        	</select></span>
                        </s:if>
                        <span>
	                        <s:if test="#request.module == 'salary_balance'">
	                        	<a href="javascript:void(0)" onclick="doSearch()" class="button"><span class="icon" style="background:url(${ctx}/images/img/zoom.png)">&nbsp;</span>确认</a>
	                        </s:if>
	                        <s:else>
		                        <input type="button" value="点击搜索"
	                                      onclick="javascript:searchClickEvent()">
	                        </s:else>
						</span>
                        </p>
                    </header>
                </s:if>
                <div id="list_content_${orgs.id}" class="clearfix"></div>
            </section>
            </s:iterator>
        </section>
    </section>
    <script type="text/javascript">
        var currentTabOrgId = ${organizationId};
        var currentSelOrgId = ${organizationId};
        $(function () {
            setTimeout('loadChildrenOrgsByTab(${organizationId})', 600);
        })

        /**
         * Tab Change that load the child org
         * @param orgId
         */
        function loadChildrenOrgsByTab(orgId) {
        	$("input[type='checkbox']").each(function() {
                $(this).attr("checked",false);
            });
        	if($("#all_select") != null){
        		$("#all_select").parent().attr("class","");
        	}
            currentTabOrgId = orgId;
            currentSelOrgId = orgId;
            //load the organization
            $.ajax({
                type:"get",
                url:"/ajax/organization-children.xhtml?parentId=" + orgId,
                //data: "name=John&location=Boston",
                dataType:"json",
                success:function (data) {
                    var strHTML = "<div id=\"uniform-undefined-" + orgId + "\" class=\"selector\">";
                    strHTML += "<span style=\"-moz-user-select: none;\">所有</span>";
                    strHTML += "<select style='opacity: 0;' parent_org_id='"+orgId+"' select_type='organization' onchange='loadChildrenOrgs(this," + orgId + ")'>";
                    strHTML += "<option value=\"-1\">所有</option>";
                    if (data != null && data.length > 0) {
                        $.each(data, function (i, org) {
                            strHTML += "<option value=\"" + org.id + "\">" + org.fullName + "</option>";
                        });
                    }
                    strHTML += "</select></div>";
                    $("#org_sel_" + orgId).html("");
                    $("#org_sel_" + orgId).append(strHTML);
                }
            });
            // load the duty
            loadDutyByOrgId(orgId);
            // load the listcontent
            loadListContent("${ctx}${listAction}", orgId);
        }

        function loadChildrenOrgs(evtObj, parentId) {
            var evtObjId = evtObj.options[evtObj.selectedIndex].value;
            $(evtObj).prev().html($(evtObj).find("option:selected").text());
            currentSelOrgId = evtObjId < 0 ? parentId : evtObjId;
            $("#uniform-undefined-" + parentId).nextAll().remove();
            $.ajax({
                type:"get",
                url:"/ajax/organization-children.xhtml?parentId=" + evtObjId,
                //data: "name=John&location=Boston",
                dataType:"json",
                success:function (data) {
                    if (data != null && data.length > 0) {
                        var strHTML = "<div id=\"uniform-undefined-" + evtObjId + "\" class=\"selector\">";
                        strHTML += "<span style=\"-moz-user-select: none;\">所有</span>";
                        strHTML += "<select style='opacity: 0;' parent_org_id='"+evtObjId+"' select_type='organization' onchange='loadChildrenOrgs(this," + evtObjId + ")'>";
                        strHTML += "<option value=\"-1\">所有</option>";
                        $.each(data, function (i, org) {
                            strHTML += "<option value=\"" + org.id + "\">" + org.fullName + "</option>";
                        });
                        strHTML += "</select></div>";
                        $("#uniform-undefined-" + parentId).after(strHTML);
                    }
                }
            });
            //load duty list
            loadDutyByOrgId(currentSelOrgId);
            //load list content
            //loadListContent(evtObjId);
        }

        function loadDutyByOrgId(orgId) {
            //load the duty
            $.ajax({
                type:"get",
                url:"/ajax/duty-list.xhtml?organizationId=" + orgId,
                //data: "name=John&location=Boston",
                dataType:"json",
                success:function (data) {
                    var strHTML = "<option value=\"-1\">所有岗位</option>";
                    if (data != null && data.length > 0) {
                        $.each(data, function (i, duty) {
                            strHTML += "<option value=\"" + duty.id + "\">" + duty.dutyName + "</option>";
                        });
                    }
                    $("#duty_sel_" + currentTabOrgId).html("");
                    $("#duty_sel_" + currentTabOrgId).append(strHTML);
                }
            });
        }

        /**
         * 加载员工
         */
        function loadEmployeework(orgId,dutyId){
        	$.ajax({
                type:"get",
                url:"/ajax/employee_list.xhtml?organizationId=" + orgId + "&dutyId=" + dutyId,
                //data: "name=John&location=Boston",
                dataType:"json",
                success:function (data) {
                    var strHTML = "<option value=\"-1\">所有人员</option>";
                    if (data != null && data.length > 0) {
                        $.each(data, function (i, work) {
                            strHTML += "<option value=\"" + work.workNO + "\">" + work.workNO + " " + work.name + "</option>";
                        });
                    }
                    $("#sel_employee_" + currentTabOrgId).html("");
                    $("#sel_employee_" + currentTabOrgId).append(strHTML);
                }
            });
        }
        
        function dutyChangeEvent(evtObj) {
            $(evtObj).prev().html($(evtObj).find("option:selected").text());
            <s:if test="#request.module == 'salary_balance'">
            	//加载员工
            	loadEmployeework(currentSelOrgId,evtObj.value);
            </s:if>
        }
        /**
         * 刷新数据
         */
        function searchClickEvent() {
            var all = false;
            /*if($("#org_sel_"+currentTabOrgId+" div").length > 1){
                var lastOrgId = $("#org_sel_"+currentTabOrgId+" div:last").find("select").val();
                if(lastOrgId == -1){
                    all = true;
                }
            }*/
            var lastOrgId = -1;
            var length = $("select[select_type='organization']").length;
            if(length > 0){
                var lastOrg = $("select[select_type='organization']").last();
                lastOrgId = $(lastOrg).val();
                if(lastOrgId == -1){
                    lastOrgId = $(lastOrg).attr("parent_org_id");
                }
            }

            //add.wxn
            var action_tag = "${action_tag}";
            if(action_tag == "TransactionAction" || action_tag == "LeaveOfficeApplyAction"){
                if(lastOrgId == undefined || lastOrgId == "undefined"){
                    lastOrgId = -1;
                }
                var data = {"searchFormPojo.startDateStr":$("#startDate").val(), "searchFormPojo.endDateStr":$("#endDate").val(), "searchFormPojo.orgId":lastOrgId, "searchFormPojo.dutyId":$("#duty_sel_"+currentTabOrgId).val(), "organizationId":currentTabOrgId, "search":"true"};
                doSearch(data);
                return;
            }
            loadListContent("${ctx}${listAction}", currentSelOrgId, all);
        }
        
        function searchAwardClickEvent(){
        	var data = {"organizationId":currentTabOrgId, "year":$("#search_year_" + currentTabOrgId).val(), "month":$("#search_month_" + currentTabOrgId).val()};
            $.ajax("${ctx}${listAction}", {
                type:"POST",
                //data:{"organizationId":organizationId},
                data:data,
                dataType:"html",
                cache:false,
                success:function (a) {
                    var obj = $("#list_content_" + currentTabOrgId);
                    obj.html("");
                    obj.append(a);
                },
                complete:function () {
                }
            });
        }

        <s:if test="#request.module == 'invite_arrange'">
        function loadListContent(url, organizationId) {
            var data = {"organizationId":organizationId, "dutyId":$("#duty_sel_" + currentTabOrgId).val(), "inviteArrangeResult":$("#arrange_sel_" + currentTabOrgId).val()};
            $.ajax(url, {
                type:"POST",
                //data:{"organizationId":organizationId},
                data:data,
                dataType:"html",
                cache:false,
                success:function (a) {
                    var obj = $("#list_content_" + currentTabOrgId);
                    obj.html("");
                    obj.append(a);
                },
                complete:function () {
                }
            });
        }
        </s:if>
        <s:elseif test="#request.module == 'qual_browse'">
        function loadListContent(url, organizationId) {
	        var data = {"organizationId":organizationId, "dutyId":$("#duty_sel_" + currentTabOrgId).val(), "type":$("#periodtype_sel_" + currentTabOrgId).val()};
	        $.ajax(url, {
	            type:"POST",
	            //data:{"organizationId":organizationId},
	            data:data,
	            dataType:"html",
	            cache:false,
	            success:function (a) {
	                var obj = $("#list_content_" + currentTabOrgId);
	                obj.html("");
	                obj.append(a);
	            },
	            complete:function () {
	            }
	        });
        }
        </s:elseif>
        <s:elseif test="#request.module == 'train'">
        function loadListContent(url, organizationId) {
        	var data = {"organizationId":organizationId, "year":$("#train_year_" + currentTabOrgId).val()};
	        $.ajax(url, {
	            type:"POST",
	            //data:{"organizationId":organizationId},
	            data:data,
	            dataType:"html",
	            cache:false,
	            success:function (a) {
	                var obj = $("#list_content_" + currentTabOrgId);
	                obj.html("");
	                obj.append(a);
	            },
	            complete:function () {
	            }
	        });
        }
        </s:elseif>
        <s:else>
        function loadListContent(url, organizationId) {
            var data = {"organizationId":organizationId, "dutyId":$("#duty_sel_" + currentTabOrgId).val()};
            //refreshListContent(url, data);
            $.ajax(url, {
                type:"POST",
                //data:{"organizationId":organizationId},
                data:data,
                dataType:"html",
                cache:false,
                success:function (a) {
                    var obj = $("#list_content_" + currentTabOrgId);
                    obj.html("");
                    obj.append(a);
                },
                complete:function () {
                }
            });
        }
        </s:else>

    </script>
</section>